//
//  MainTabBarController.m
//  PASystem
//
//  Created by michael on 16/7/19.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "MainTabBarController.h"
#import "buttonitem.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "OABaseNavViewController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//        self.tabBar.tintColor = [UIColor blueColor];
        [self _initViewControllers];

    [self _customTabar];


    // Do any additional setup after loading the view.
}

//01创建所有的子视图控制器
- (void)_initViewControllers{
    ViewController1 *vc1 = [[ViewController1 alloc]init];
    ViewController2 *readvc = [[ViewController2 alloc]init];
    ViewController3 *lisvc = [[ViewController3 alloc]init];
        NSArray *viewCtrls = @[vc1,readvc,lisvc];
    // 创建一个可变数组存放所有的导航控制器
   _navCtrls = [[NSMutableArray alloc] init];

    
    for (UIViewController *viewCtrl in viewCtrls) {
        // 创建导航控制器
        OABaseNavViewController *baseNavCtrl = [[OABaseNavViewController alloc] initWithRootViewController:viewCtrl];
        baseNavCtrl.delegate = self;
        
        // 导航控制器添加到数组中
        [_navCtrls addObject:baseNavCtrl];
        
    }
    self.viewControllers = _navCtrls;

}
    

             
             
//02 定制标签栏
- (void)_customTabar
{
    // 1 影藏
    //self.tabBar.translucent = NO;

    self.tabBar.hidden=YES;
    // 2 创建自定义标签视图，选中标签栏选项视图
    _tabBarImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, kScreenHeight-49, kScreenWidth, 49)];
    _tabBarImageview.backgroundColor = [UIColor blueColor];

    
    _tabBarImageview.userInteractionEnabled = YES;
    [self.view addSubview:_tabBarImageview];

       //3 创建5个按钮
    //tabbar_icon_found_normal@2x.png
    NSArray *imageNames = @[@"贸促数据库",@"个人办公",@"个人"];
    
    
     NSArray *titles = @[@"贸促数据库",@"个人办公",@"个人"];
    // 循环创建
    float itemWidth = kScreenWidth / imageNames.count;
    for (int i = 0 ; i < imageNames.count; i++) {
        UIButton *barItem = [[UIButton alloc] initWithFrame:CGRectMake(i * itemWidth, 0, itemWidth, 49)];
        // 设置标题
      //  barItem.title = titles[i];
        // 设置图片
        [barItem setImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
//        [barItem setImage:[UIImage imageNamed:hightimageNames[i]] forState:UIControlStateSelected];
        [barItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        barItem.titleLabel.font= [UIFont boldSystemFontOfSize:12];
//        [barItem setTitleEdgeInsets:UIEdgeInsetsMake(33,-17, 0, 20)];

        [barItem setTitle:titles[i] forState:UIControlStateNormal];

        
        // 设置事件
        barItem.tag = 10+i;
        if (barItem.tag==11) {
            [barItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            barItem.selected = YES;
        }
    

        [barItem addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];
        [_tabBarImageview addSubview:barItem];
    }
    //默认选中的中间的控制器
self.selectedIndex = 1;

}
- (void)itemAction:(UIButton *)button{
        self.selectedIndex = button.tag-10;
        if (_index !=self.selectedIndex)
    {
        UIButton *button1 = (UIButton *)[_tabBarImageview viewWithTag:self.selectedIndex+10];
        button1.selected = YES;
        UIButton *button2 = (UIButton *)[_tabBarImageview viewWithTag:_index+10];
        [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button2.selected = NO;
    
    }
    if (button.selected) {
        _index = self.selectedIndex;

}

}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
int count = (int)navigationController.viewControllers.count;
    if (count==1) {
    
    viewController.tabBarController.tabBar.hidden =YES;

        _tabBarImageview.hidden = NO;
       
                }
    else {
        //是push进来的视图控制器
        _tabBarImageview.hidden=NO;

    }
}



   
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
